package com.swfee.qinben;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.swfee.qinben.adpt.CommentAdapter;
import com.swfee.qinben.adpt.MedicalRecordAdapter;
import com.swfee.qinben.adpt.TestResultAdapter;
import com.swfee.qinben.base.TabViewflipperBaseActivity;
import com.swfee.qinben.dto.active.CommentModel;
import com.swfee.qinben.dto.active.MedicalRecordModel;
import com.swfee.qinben.dto.active.ScheduleModel;
import com.swfee.qinben.dto.req.BindEquipementReqDto;
import com.swfee.qinben.dto.rsp.CommentRspDto;
import com.swfee.qinben.dto.rsp.HealthMeasureRspDto;
import com.swfee.qinben.dto.rsp.MedicalHistoryRspDto;
import com.swfee.qinben.httpactions.CommentAction;
import com.swfee.qinben.httpactions.FamilyAction;
import com.swfee.qinben.util.CurenActivitUtils;

public class ScheduleResultCommentActivity extends TabViewflipperBaseActivity {

	
	List<CommentModel> listComments =null;
	List<MedicalRecordModel> listMedicalRecords = null;
	List<HealthMeasureRspDto> listTestResults = null;
	
	

	Handler handle = new Handler();
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTabTitle("检测结果","评论","病历");
		initListViews();

	}
	
	public void  initListViews(){
		final ListView listViewTest = (ListView) findViewById(R.id.listView_test);
		final ListView listViewComment = (ListView) findViewById(R.id.listView_comment);
		final ListView listViewMedicalRecord = (ListView) findViewById(R.id.listView_medical_record);
		///
		final LinearLayout writeMedicalHistoryLay  =   (LinearLayout) findViewById(R.id.write_medical_history_lay);
		final LinearLayout writeCommentLay  =   (LinearLayout) findViewById(R.id.write_comment_lay);
		writeCommentLay.setVisibility(View.GONE);
		writeMedicalHistoryLay.setVisibility(View.GONE);
		TextView writeComment =  (TextView) findViewById(R.id.write_comment);
		TextView writeMedicalHistory =  (TextView) findViewById(R.id.write_medical_history);
		writeComment.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				writeCommentLay.setVisibility(View.VISIBLE);
				listViewComment.setVisibility(View.GONE);
			}
		});
		writeMedicalHistory.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				listViewMedicalRecord.setVisibility(View.GONE);
				writeMedicalHistoryLay.setVisibility(View.VISIBLE);
			}
		});
		
		
		showProgressDialog("加载检测及评论数据..");
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {

				ScheduleModel scheduleModel =  getIntent().getParcelableExtra("scheduleModel");
				BindEquipementReqDto bindEquipementReqDto =  getIntent().getParcelableExtra("bindEquipementReqDto");
//				HealthMeasureReqDto healthMeaReqDto = new HealthMeasureReqDto();
//				healthMeaReqDto.setEquipmentId(bindEquipementReqDto.getEquipmentId());
//				healthMeaReqDto.setFamilyId(bindEquipementReqDto.getFamilyId());
//				healthMeaReqDto.setScheduleId(bindEquipementReqDto.getFamilyId());
//				HealthMeasureRspDto healthMeasureRspDto = new ScheduleAction().getHealthMeasureData(healthMeaReqDto);
				
				Integer scheduleId = -1; 
				Integer familyId = -1; 
				Integer equipmentId = -1;
				
				if(scheduleModel!=null){
					scheduleId = scheduleModel.getId();
					familyId = scheduleModel.getFamilyId();
				}else{
					scheduleId = bindEquipementReqDto.getScheduleId();
					familyId = bindEquipementReqDto.getFamilyId();
					equipmentId = bindEquipementReqDto.getEquipmentId();
				}
				
				
				
				List<CommentRspDto> commentRspDtoList =  new CommentAction().getComments(scheduleId);
				
				 listComments = new ArrayList<CommentModel>();
				for(CommentRspDto m1:commentRspDtoList){
					
					CommentModel m  = 	new CommentModel(m1.getCommentUser(),m1.getContent() , m1.getCommentDate());
					m.setImgUrl(m1.getImageUrl());
					m.setVedioUrl(m1.getVideoUrl());
					m.setReplyList(m1.getReplyList());
					listComments.add(m);
				}
				
				
				
				 listMedicalRecords = new ArrayList<MedicalRecordModel>();
				 FamilyAction familyAction =  new FamilyAction();
				 List<MedicalHistoryRspDto> medciaHRspDtoList  = familyAction.getMedicalHistory(familyId);
				 
				
				 
					for(MedicalHistoryRspDto m1:medciaHRspDtoList){
						listMedicalRecords.add(new MedicalRecordModel(m1.getDoctorName(), m1.getSubmitDate(), m1.getRecord()));
					}
					
					listTestResults = new ArrayList<HealthMeasureRspDto>();
					HealthMeasureRspDto heaMDRspDto = familyAction.getHealthMeasureData(scheduleId, familyId);
					listTestResults.add(heaMDRspDto);
				
				
				handle.post(new Runnable() {
					
					@Override
					public void run() {
						hideProgressDialog();
						Context curCtx = CurenActivitUtils.getCurActivity();
						MedicalRecordAdapter adapter3 = new MedicalRecordAdapter(listMedicalRecords, curCtx);
						CommentAdapter adapter2 = new CommentAdapter(listComments, curCtx);
						TestResultAdapter adapter1 = new TestResultAdapter(listTestResults, curCtx);
						listViewTest.setAdapter(adapter1);
						listViewComment.setAdapter(adapter2);
						listViewMedicalRecord.setAdapter(adapter3);
					}
					
				});
				
				
			}
		}).start();
		
	
	}
	
	
	
	@Override
	public int getLayoutId() {
		return R.layout.schedule_opt_comment;
	}

}